from flask import Flask, request, render_template_string

app = Flask(__name__)

# 页面模板
html_template = '''
<!DOCTYPE html>
<html>
<head>
    <title>基础代谢计算器</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f8fa;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 400px;
            width: 96vw;
            margin: 60px auto;
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            padding: 32px 6vw 24px 6vw;
            box-sizing: border-box;
        }
        h1 {
            text-align: center;
            color: #2d3a4b;
            margin-bottom: 28px;
            font-size: 1.6em;
        }
        label {
            display: block;
            margin-bottom: 6px;
            color: #444;
            font-size: 1em;
        }
        input, select {
            width: 100%;
            padding: 8px 10px;
            margin-bottom: 18px;
            border: 1px solid #d1d5db;
            border-radius: 6px;
            font-size: 1em;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            background: #0078e7;
            color: #fff;
            border: none;
            border-radius: 6px;
            padding: 10px 0;
            font-size: 1.1em;
            cursor: pointer;
            transition: background 0.2s;
        }
        button:hover {
            background: #005bb5;
        }
        h2 {
            text-align: center;
            color: #0078e7;
            margin-top: 24px;
            font-size: 1.2em;
        }
        @media (max-width: 500px) {
            .container {
                margin: 16px auto;
                padding: 18px 2vw 14px 2vw;
            }
            h1 {
                font-size: 1.2em;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>基础代谢计算器</h1>
        <form method="post">
            <label for="gender">性别：</label>
            <select id="gender" name="gender" required>
                <option value="male" {% if gender == 'male' %}selected{% endif %}>男</option>
                <option value="female" {% if gender == 'female' %}selected{% endif %}>女</option>
            </select>
            <label for="age">年龄（岁）：</label>
            <input type="number" id="age" name="age" min="0" required value="{{ age|default('') }}">
            <label for="height">身高（cm）：</label>
            <input type="number" step="0.1" id="height" name="height" min="0" required value="{{ height|default('') }}">
            <label for="weight">体重（kg）：</label>
            <input type="number" step="0.1" id="weight" name="weight" min="0" required value="{{ weight|default('') }}">
            <button type="submit">计算</button>
        </form>
        {% if result %}
            <h2>基础代谢率：{{ result }} 千卡/天</h2>
        {% endif %}
    </div>
</body>
</html>
'''

@app.route('/', methods=['GET', 'POST'])
def calculator():
    result = None
    gender = None
    age = ''
    height = ''
    weight = ''
    if request.method == 'POST':
        gender = request.form['gender']
        age = request.form['age']
        height = request.form['height']
        weight = request.form['weight']
        try:
            age_f = float(age)
            height_f = float(height)
            weight_f = float(weight)
            # 计算基础代谢率（Mifflin-St Jeor 公式）
            if gender == 'male':
                bmr = (10 * weight_f) + (6.25 * height_f) - (5 * age_f) + 5
            else:
                bmr = (10 * weight_f) + (6.25 * height_f) - (5 * age_f) - 161
            result = round(bmr, 2)
        except Exception:
            result = None
    return render_template_string(html_template, result=result, gender=gender, age=age, height=height, weight=weight)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
